package com.icity.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.icity.common.Result;
import com.icity.entity.EchartLine;
import com.icity.entity.Partnerstatistical;
import com.icity.entity.TrendsUser;
import com.icity.entity.UserDistribution;
import com.icity.entity.UserTrends;
import com.icity.entity.system.TableSupport;
import com.icity.service.TrendsUserService;
import com.icity.util.DateUtils;

/**
 * 用户趋势
 *
 * @author Administrator
 *
 */
@RestController
@RequestMapping(value = "/admin/usertrends")
public class UserTrendsController {

	@Autowired
	private TrendsUserService trendsUserService;

	@RequestMapping(value = "/trendline")
	@ResponseBody
	public Result getArticelLine(HttpServletRequest request) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Map<Object,Object> params = new HashMap<Object,Object>();
		String type = request.getParameter("type");
		params.put("type", type);
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)) {
			params.put("startDate", startDate);
			params.put("endDate", endDate);
		} else {
			params.put("nowDate", sdf.format(new Date()));
			params.put("startDate", DateUtils.getSpecifiedDayBefore(sdf.format(new Date()), 7));
			params.put("endDate", sdf.format(new Date()));
		}
		Result result = new Result();
		try {
			EchartLine echartLine = new EchartLine();
			Map<Object,String> tooltip = trendsUserService.getTooltip();
			echartLine.setTooltip(tooltip);

			Map<Object,List<String>> legend = trendsUserService.getLegend();
			echartLine.setLegend(legend);

			Map<Object,Object> grid = trendsUserService.getGrid();
			echartLine.setGrid(grid);

			Map<Object,Object> xAxis = trendsUserService.getWeekDayMap(params, 7);
			echartLine.setxAxis(xAxis);

			Map<Object,Object> yAxis = trendsUserService.getYaxis();
			echartLine.setyAxis(yAxis);

			List<Map<Object,Object>> listSeries = trendsUserService.getTrendsUserSeries(params);
			echartLine.setSeries(listSeries);

			result.setData(echartLine);
			result.setCode("1");
			result.setMessage("获取数据成功");
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode("-1");
			result.setMessage("获取数据异常");
			return result;
		}
	}

	// 数据详情
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/datadetails")
	@ResponseBody
	public Result getUserTrendsDataDetails(HttpServletRequest request) {
		int page = Integer.parseInt(request.getParameter("page"));
		int limit = Integer.parseInt(request.getParameter("limit"));
		Map<Object, Object> map = new HashMap<Object, Object>();
		Result result = new Result();
		try {
			// map.put("role_code", 1);
			map.put("page", (page - 1) * limit);
			map.put("limit", limit);
			TableSupport<TrendsUser> list = (TableSupport<TrendsUser>) trendsUserService.getLsit(map);
			result.setCode("1");
			result.setMessage("获取成功");
			result.setData(list);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode("-1");
			result.setMessage("获取数据异常");
			return result;
		}
	}

	@RequestMapping(value = "/info")
	@ResponseBody
	public Result getUserTrendInfo() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		String day = DateUtils.getSpecifiedDayBefore(sdf.format(date), 1);
		Result result = new Result();
		try {
			UserTrends userTrends = trendsUserService.getUserTrends(day);
			result.setCode("1");
			result.setMessage("获取成功");
			result.setData(userTrends);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode("-1");
			result.setMessage("获取数据异常");
			return result;
		}
	}

	@RequestMapping(value = "/serchdatadata")
	@ResponseBody
	public Result serchdatadata(HttpServletRequest request) {
		Result result = new Result();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Map<Object, Object> params = new HashMap<Object, Object>();
		String param = request.getParameter("params");
		// if (StringUtils.isNotBlank(param)) {
		params.put("param", param);
		// 点击为活跃用户或默认加载出活跃用户时
		if (param.equals("1") || param == "") {
			String startDate = request.getParameter("startDate");
			String endDate = request.getParameter("endDate");
			if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)) {
				params.put("startDate", startDate);
				params.put("endDate", endDate);
			} else {
				params.put("nowDate", sdf.format(new Date()));
				params.put("startDate", DateUtils.getSpecifiedDayBefore(sdf.format(new Date()), 7));
				params.put("endDate", sdf.format(new Date()));
			}
			try {
				List<Object> liste = new ArrayList<>();
				// 查询出日期
				List<String> data = trendsUserService.getWeekDay(params, 7);
				// 根据日期查询出数量
				List<Integer> lists = trendsUserService.getdatanum(params);
				liste.add(data);
				liste.add(lists);
				result.setData(liste);
				result.setCode("1");
				result.setMessage("获取数据成功");
				return result;
			} catch (Exception e) {
				result.setCode("-1");
				result.setMessage("获取数据异常");
				return result;
			}
		} else if (param.equals("2")) {
			// 点击为新增注册账号
			String startDate = request.getParameter("startDate");
			String endDate = request.getParameter("endDate");
			if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)) {
				params.put("startDate", startDate);
				params.put("endDate", endDate);
			} else {
				params.put("nowDate", sdf.format(new Date()));
				params.put("startDate", DateUtils.getSpecifiedDayBefore(sdf.format(new Date()), 7));
				params.put("endDate", sdf.format(new Date()));
			}
			try {
				List<Object> liste = new ArrayList<>();
				// 查询出日期
				List<String> data = trendsUserService.getWeekDay(params, 7);
				// 根据日期查询出数量
				List<Integer> lists = trendsUserService.getaddusernum(params);
				liste.add(data);
				liste.add(lists);
				result.setData(liste);
				result.setCode("1");
				result.setMessage("获取数据成功");
				return result;
			} catch (Exception e) {
				result.setCode("-1");
				result.setMessage("获取数据异常");
				return result;
			}
		} else if (param.equals("3")) {
			// 当点击为启动次数时
			String startDate = request.getParameter("startDate");
			String endDate = request.getParameter("endDate");
			if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)) {
				params.put("startDate", startDate);
				params.put("endDate", endDate);
			} else {
				params.put("nowDate", sdf.format(new Date()));
				params.put("startDate", DateUtils.getSpecifiedDayBefore(sdf.format(new Date()), 7));
				params.put("endDate", sdf.format(new Date()));
			}
			try {
				List<Object> liste = new ArrayList<>();
				// 查询出日期
				List<String> data = trendsUserService.getWeekDay(params, 7);
				// 根据日期查询出数量
				List<Integer> lists = trendsUserService.getstartnum(params);
				liste.add(data);
				liste.add(lists);
				result.setData(liste);
				result.setCode("1");
				result.setMessage("获取数据成功");
				return result;
			} catch (Exception e) {
				result.setCode("-1");
				result.setMessage("获取数据异常");
				return result;
			}
		} else if (param.equals("4")) {
			// 当点击为启动次数时
			String startDate = request.getParameter("startDate");
			String endDate = request.getParameter("endDate");
			if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)) {
				params.put("startDate", startDate);
				params.put("endDate", endDate);
			} else {
				params.put("nowDate", sdf.format(new Date()));
				params.put("startDate", DateUtils.getSpecifiedDayBefore(sdf.format(new Date()), 7));
				params.put("endDate", sdf.format(new Date()));
			}
			try {
				List<Object> liste = new ArrayList<>();
				// 查询出日期
				List<String> data = trendsUserService.getWeekDay(params, 7);
				// 根据日期查询出数量
				List<Integer> lists = trendsUserService.getstartnum(params);
				liste.add(data);
				liste.add(lists);
				result.setData(liste);
				result.setCode("1");
				result.setMessage("获取数据成功");
				return result;
			} catch (Exception e) {
				result.setCode("-1");
				result.setMessage("获取数据异常");
				return result;
			}
		}
		return result;
	}

	// 用户画像性别分布
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/GenderDistribution")
	@ResponseBody
	public Result getGenderDistribution(HttpServletRequest request) {
		Result result = new Result();
		try {
			List<Map<Object, Object>> list = new ArrayList<Map<Object, Object>>();
			Map<Object, Object> map = new HashMap<Object, Object>();
			// 查询出APPuser男性用户
			List<Integer> mancount = trendsUserService.selectappuserman();
			map.put("value", mancount);
			map.put("name", "男性");
			list.add(map);
			// 查询出APPuser女性用户
			Map<Object, Object> mapwoman = new HashMap<Object, Object>();
			List<Integer> womancount = trendsUserService.selectappuserwoman();
			mapwoman.put("value", womancount);
			mapwoman.put("name", "女性");
			list.add(mapwoman);
//			// 查询出APPuser未知用户
//			Map<Object, Object> unknownmap = new HashMap<Object, Object>();
//			List<Integer> unknowncount = trendsUserService.selectappuserunknown();
//			unknownmap.put("value", unknowncount);
//			unknownmap.put("name", "未知用户");
//			list.add(unknownmap);
			result.setCode("1");
			result.setMessage("获取成功");
			result.setData(list);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode("-1");
			result.setMessage("获取数据异常");
			return result;
		}
	}

	/**
	 * 查询设备位置分布
	 */
	@RequestMapping(value = "/getUserChinaDistribution")
	// @PostMapping(value="/getCount")
	@ResponseBody
	public Result getUserChinaDistribution(HttpServletRequest request) {
		Result result = new Result();
		try {
			Map<Object, Object> map = new HashMap<>();
			// 获取选择的合伙人
			String partner_id = request.getParameter("partner_id");
			if (StringUtils.isNotBlank(partner_id)) {
				map.put("partner_id", partner_id);
			}
			// 获取选择的应用唯一标识
			String application_unique_code = request.getParameter("application_unique_code");
			if (StringUtils.isNotBlank(application_unique_code)) {
				map.put("application_unique_code", application_unique_code);
			}
			String type = request.getParameter("type");
			if (StringUtils.isNotBlank(type)) {
				map.put("type", type);
			}
			List<UserDistribution> list = trendsUserService.getUserChinaDistribution(map);
			List<Map<Object, Object>> lists = new ArrayList<>();
			for (UserDistribution equipmentDistribution : list) {
				Map<Object, Object> maps = new HashMap<>();
				maps.put("name", equipmentDistribution.getProvince());
				maps.put("value", equipmentDistribution.getLocation_num());
				lists.add(maps);
			}
			if (lists != null && lists.size() > 0) {
				result.setMessage("获取成功");
				result.setCode("1");
				result.setData(lists);
			} else {
				result.setCode("-1");
				result.setData(new ArrayList<>());
				result.setMessage("暂无数据");
			}
		} catch (Exception e) {
			result = new Result();
			result.setCode("-1");
			result.setMessage("获取数据异常");
		}
		return result;
	}

	/**
	 * 查询用户省位置分布
	 */
	@RequestMapping(value = "/getUserDistribution")
	// @PostMapping(value="/getCount")
	@ResponseBody
	public Result getUserDistribution(HttpServletRequest request) {
		Result result = new Result();
		try {

			Map<Object, Object> map = new HashMap<>();
			// 获取选择的合伙人
			String partner_id = request.getParameter("partner_id");
			if (StringUtils.isNotBlank(partner_id)) {
				map.put("partner_id", partner_id);
			}
			// 获取选择的应用唯一标识
			String application_unique_code = request.getParameter("application_unique_code");
			if (StringUtils.isNotBlank(application_unique_code)) {
				map.put("application_unique_code", application_unique_code);
			}
			// 安卓或者IOS
			String type = request.getParameter("type");
			if (StringUtils.isNotBlank(type)) {
				map.put("type", type);
			}
			List<Object> liste = new ArrayList<>();
			// 查询出省份
			List<String> list = trendsUserService.getUserDistribution(map);
			// 查询出省份数量
			List<Integer> lists = trendsUserService.getUserProvinceNumDistribution(map);
			liste.add(list);
			liste.add(lists);

			if (lists != null && lists.size() > 0) {
				result.setMessage("获取成功");
				result.setCode("1");
				result.setData(liste);
			} else {
				result.setCode("-1");
				result.setData(new ArrayList<>());
				result.setMessage("暂无数据");
			}
		} catch (Exception e) {
			result = new Result();
			result.setCode("-1");
			result.setMessage("获取数据异常");
		}
		return result;
	}

	@RequestMapping(value = "/getUserCityNUmDistribution")
	@ResponseBody
	public Result getEquipmentcityDistribution(HttpServletRequest request) {
		Result result = new Result();
		try {
			Map<Object, Object> map = new HashMap<>();
			// 获取选择的合伙人
			String partner_id = request.getParameter("partner_id");
			if (StringUtils.isNotBlank(partner_id)) {
				map.put("partner_id", partner_id);
			}
			// 获取选择的应用唯一标识
			String application_unique_code = request.getParameter("application_unique_code");
			if (StringUtils.isNotBlank(application_unique_code)) {
				map.put("application_unique_code", application_unique_code);
			}
			String type = request.getParameter("type");
			if (StringUtils.isNotBlank(type)) {
				map.put("type", type);
			}
			List<Object> liste = new ArrayList<>();
			// 查询出省份
			List<String> list = trendsUserService.getUsercityDistribution(map);
			// 查询出省份数量
			List<Integer> lists = trendsUserService.getUserNumDistributioncity(map);
			liste.add(list);
			liste.add(lists);
			if (liste != null && liste.size() > 0) {
				result.setMessage("获取成功");
				result.setCode("1");
				result.setData(liste);
			} else {
				result.setCode("-1");
				result.setData(new ArrayList<>());
				result.setMessage("暂无数据");
			}

		} catch (Exception e) {
			result = new Result();
			result.setCode("-1");
			result.setMessage("获取数据异常");
		}

		return result;
	}

	// 合伙人数据详情
	@RequestMapping(value = "/partnertable")
	@ResponseBody
	public Result getMenuList(HttpServletRequest request) {
		int page = Integer.parseInt(request.getParameter("page"));
		int limit = Integer.parseInt(request.getParameter("limit"));
		String name = request.getParameter("name");

		Map<Object, Object> map = new HashMap<Object, Object>();

		if (!name.equals("undefined")) {
			map.put("name", name);
		}

		Result result = new Result();
		try {

			map.put("page", (page - 1) * limit);
			map.put("limit", limit);
			TableSupport<Partnerstatistical> list = (TableSupport<Partnerstatistical>) trendsUserService
					.getPartnertable(map);
			result.setCode("1");
			result.setMessage("获取成功");
			result.setData(list);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode("-1");
			result.setMessage("获取数据异常");
			return result;
		}
	}

}
